// @ts-check
import withNuxt from './.nuxt/eslint.config.mjs'
import stylistic from '@stylistic/eslint-plugin'
import prettier from 'eslint-config-prettier'

export default withNuxt([
  prettier,
  {
    plugins: {
      '@stylistic': stylistic as any
    },
    rules: {
      /** *********************
       * 核心规则 (ESLint内置)
       ***********************/
      'no-unused-vars': 'off', // 禁用基础规则
      'no-console': process.env.NODE_ENV === 'production' ? 'warn' : 'off',
      'no-debugger': process.env.NODE_ENV === 'production' ? 'warn' : 'off',
      'prefer-const': 'off',
      'import/no-mutable-exports': 'off',
      'block-spacing': ['error', 'always'],
      'brace-style': ['error', '1tbs', { allowSingleLine: true }],

      /** *********************
       * TypeScript规则
       ***********************/
      'vue/no-export-in-script-setup': 'off',
      '@typescript-eslint/no-unused-vars': [
        'error',
        {
          argsIgnorePattern: '^_',
          varsIgnorePattern: '^_'
        }
      ],
      '@typescript-eslint/no-explicit-any': 'off',
      '@typescript-eslint/no-unused-expressions': 'off',
      '@typescript-eslint/no-invalid-void-type': 'off',

      /** *********************
       * Stylistic格式化规则
       ***********************/
      '@stylistic/quotes': [
        'error',
        'single',
        {
          avoidEscape: true,
          allowTemplateLiterals: true
        }
      ],
      '@stylistic/semi': ['error', 'never'],
      '@stylistic/comma-spacing': ['error', { before: false, after: true }],
      '@stylistic/comma-dangle': ['error', 'never'],
      '@stylistic/comma-style': ['error', 'last'],
      '@stylistic/key-spacing': ['error', { beforeColon: false, afterColon: true }],
      '@stylistic/keyword-spacing': ['error', { before: true, after: true }],
      '@stylistic/space-before-blocks': ['error', 'always'],
      '@stylistic/space-in-parens': ['error', 'never'],
      '@stylistic/space-infix-ops': 'error',
      '@stylistic/space-unary-ops': ['error', { words: true, nonwords: false }],
      '@stylistic/arrow-spacing': ['error', { before: true, after: true }],
      '@stylistic/indent': [
        'error',
        2,
        {
          SwitchCase: 1,
          ignoredNodes: ['TemplateLiteral'],
          offsetTernaryExpressions: true
        }
      ],
      '@stylistic/object-curly-spacing': ['error', 'always'],
      '@stylistic/jsx-indent': ['error', 2],
      '@stylistic/space-before-function-paren': [
        'error',
        {
          anonymous: 'never',
          named: 'never',
          asyncArrow: 'always'
        }
      ],

      /** *********************
       * Vue专用规则
       ***********************/
      // 'vue/attributes-order': 'off',
      'vue/attributes-order': [
        'error',
        {
          order: ['DEFINITION', 'LIST_RENDERING', 'CONDITIONALS', 'RENDER_MODIFIERS', 'GLOBAL', 'UNIQUE', 'TWO_WAY_BINDING', 'OTHER_DIRECTIVES', 'OTHER_ATTR', 'EVENTS', 'CONTENT'],
          alphabetical: true // 按字母顺序排序
        }
      ],
      'vue/html-self-closing': [
        'error',
        {
          html: {
            void: 'always', // 空元素（如<img>）必须自闭合
            normal: 'always', // 普通元素（如<div>）无内容时必须自闭合
            component: 'always' // Vue组件无内容时必须自闭合
          },
          svg: 'always',
          math: 'always'
        }
      ],
      'vue/v-on-event-hyphenation': 'off',
      'vue/no-multiple-template-root': 'off',
      'vue/no-v-html': 'off',
      'vue/require-default-prop': 'off',
      'vue/multi-word-component-names': 'off',
      'vue/require-explicit-emits': ['error', { allowProps: true }],
      'vue/no-multi-spaces': ['error', { ignoreProperties: false }],
      'vue/no-template-target-blank': 'error',

      /** *********************
       * 模板格式化规则
       ***********************/
      'vue/html-indent': [
        'error',
        2,
        {
          attribute: 1,
          baseIndent: 1,
          closeBracket: 0,
          alignAttributesVertically: true
        }
      ],
      'vue/html-closing-bracket-newline': [
        'error',
        {
          singleline: 'never',
          multiline: 'always'
        }
      ]
    }
  },

  /** *********************
   * Vue文件特定配置
   ***********************/
  {
    files: ['**/*.vue'],
    rules: {
      'vue/max-attributes-per-line': [
        'error',
        {
          singleline: 3,
          multiline: { max: 1 }
        }
      ],
      'vue/first-attribute-linebreak': [
        'error',
        {
          singleline: 'ignore',
          multiline: 'below'
        }
      ],
      'vue/multiline-html-element-content-newline': [
        'error',
        {
          ignoreWhenEmpty: true,
          ignores: ['pre', 'textarea']
        }
      ]
    }
  }
])
